<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/11
 * Time: 16:37
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\service\MiniEvent;
use app\api\service\Token;
use app\api\service\ThirdApp as ThirdAppService;
use app\api\model\ThirdApp as ThirdAppModel;
use app\lib\exception\MiniSuiteException;
use app\lib\exception\SuccessMessage;
use app\lib\exception\UserException;


class ThirdApp extends BaseController
{
    /**
     * 获取名片设置信息
     * @return string
     */
    public function getAppInfo(){
        $t_id = Token::getCurrentTokenVar('manager_id');
        $service = new  ThirdAppService();
        $authInfo = $service->getAppInfo($t_id);
        return json_encode($authInfo);
    }
    /**
     * 获取用户授权列表
     * @return \think\response\Json
     */
    public function getUserInfo(){
        $uid = Token::getCurrentUid();
        $AppInfo = new  ThirdAppService();
        $authInfo = $AppInfo->getAuthInfo($uid);
        return json($authInfo);
    }
    /**
     * 获取用户登录信息
     * @throws UserException
     */
    public function getThirdAppInfo(){
        $uid = Token::getCurrentUid();
        $userInfo = ThirdAppModel::getUserInfo($uid);
        $userInfo->visible(['app_id','name','sub_name','address','card_num','create_time','end_time','work','img','workuser']);
        $userInfo->work->visible(['corp_name','corp_type','corp_full_name','subject_type','corp_wxqrcode']);
        $data =  $userInfo->toArray();
        $data['workuser'] =  count($userInfo->workuser);
        $data['end_time'] = date('Y-m-d H:i:s',$data['end_time']);
        if (!$data){
            throw new UserException();
        }
        return json_encode($data);
    }

    /**
     * 修改密码
     * @param $oldPassword
     * @param $newPassword
     * @return $this
     * @throws UserException
     */
    public function changePassword($oldPassword,$newPassword){
        $t_id = Token::getCurrentUid();
        $thirdAppInfo = new ThirdAppService();
        $result =  $thirdAppInfo->changePassword($t_id,$oldPassword,$newPassword);
        if (!$result){
            throw new UserException([
                'code'=>203,
                'msg'=>'修改密码失败',
                'error_code'=>60020
            ]);
        }
        return json(new SuccessMessage(),201);
    }

    /**
     * 获取名片统计信息
     * @return string
     * @throws UserException
     */
    public function getWorkUserInfo(){
        $uid = Token::getCurrentUid();
        $thirdAppInfo = new ThirdAppService();
        $info = $thirdAppInfo->getWorkUserInfo($uid);
        if (!$info){
            throw new UserException();
        }
        return json_encode($info);
    }

    /**
     * 获取账号统计信息
     * @return string
     * @throws UserException
     */
    public function getAccountStatus(){
        $uid = Token::getCurrentUid();
        $thirdAppInfo = new ThirdAppService();
        $info = $thirdAppInfo->getAccountStatus($uid);
        if (!$info){
            throw new UserException();
        }
        return json_encode($info);
    }

    /**
     *获取用户数量
     * @return string
     * @throws UserException
     */
    public function getUserNum(){
        $uid  = Token::getCurrentUid();
        $thirdApp = new ThirdAppService();
        $info =  $thirdApp->getUserNum($uid);
        if (!$info){
            throw new UserException([
                'code'=>203,
                'msg'=>'还没有用户',
                'error_code'=>60030
            ]);
        }
        return json_encode($info);
    }

    /**
     * 统计信息
     * @param $time
     * @param $type
     * @return string
     * @throws UserException
     */
    public function getCountInfo($time,$type){
        $uid = Token::getCurrentUid();
        $service = new ThirdAppService();
        $result =  $service->getData($uid,$type,$time);
        if (!$result){
            throw new UserException();
        }
        return json_encode($result);
    }

    /**
     * 根基时间获取行为统计
     * @param $time
     * @param $type
     * @return false|int|\PDOStatement|string|\think\Collection
     * @throws UserException
     */
    public function getCountInfoByTime($time,$type){
        $uid = Token::getCurrentUid();
        $service = new ThirdAppService();
        $result =  $service->getCountData($uid,$type,$time);
        if (!$result){
            throw new UserException();
        }
        return json_encode($result);
    }

    /**
     *更新用户信息
     * @return \think\response\Json
     * @throws UserException
     */
    public function updateThirdAppInfo(){
        $uid = Token::getCurrentUid();
        $data=input('post.');
        $result = ThirdAppModel::updateUserInfo($uid,$data);
        if (!$result){
            throw new UserException();
        }
        return json(new SuccessMessage(),201);
    }
    /**
     * 获取官网信息
     * @return mixed
     * @throws UserException
     */
    public function getWebInfo(){
        $uid = Token::getCurrentUid();
        $thirdApp = new ThirdAppService();
        $result =  $thirdApp->getUserInfo($uid);
        if (!$result ){
            throw new UserException();
        }
        return $result['web_info'];
    }
    /**
     * 更新用户信息
     * @return \think\response\Json
     * @throws UserException
     */
    public function updateWebInfo(){
        $uid = Token::getCurrentUid();
        $data = input('post.');
        $web_info['web_info'] =json_encode($data['web_info']);
        list($item, $result) = $this->updateWebItem($data, $uid, $web_info);
        //删除图片
            if ($data['delImg'] != null){
                try {
                    $file = new File();
                    foreach ($data['delImg'] as $item){
                      $file->deleteImg($item['img_id'],$item['type']);
                    }
                }catch (\Exception $exception){
                    throw new UserException([
                        'code'=>404,
                        'msg'=>$exception,
                        'error_code'=>60010
                    ]);
                }
            }
        if (!$result){
            throw new UserException([
                'code'=>201,
                'msg'=>"更新失败",
                'error_code'=>60010
            ]);
        }
        //修改成功后清空缓存
        $tag = sprintf(config('cache.web'),$uid);
        //缓存的处理
        $this->removeCache($tag);
        return json(new SuccessMessage(),201);
    }
    /**
     * 更新官网信息
     * @param $data
     * @param $uid
     * @param $web_info
     * @return array
     */
    public function updateWebItem($data, $uid, $web_info): array
    {
        $sub_web = [];
        $sub_info= [];
        foreach ($data['web_info'] as $value) {
            if ($value['name'] == 'qyzx') {
                foreach ($value['content']['content'] as $key => $item) {
                    $sub_web[$key]['style'] = $value['name'];
                    $sub_web[$key]['title'] = $item['messageTitle'];
                    $sub_web[$key]['sub_id'] = $item['messageId'];
                    $sub_web[$key]['content'] = json_encode($item['messageContern']);
                    $sub_web[$key]['t_id'] = $uid;
                }
            }
            if ($value['name'] == 'qyzp'){
                foreach ($value['content']['text'] as $key=>$item){
                    $sub_info[$key]['style'] = $value['name'];
                    $sub_info[$key]['title'] = $item['JobsZWInput'];
                    $sub_info[$key]['sub_id'] = $item['Jobsid'];
                    $sub_info[$key]['num'] = $item['JobsRSInput'];
                    $sub_info[$key]['salary'] = $item['JobsXZInput'];
                    $sub_info[$key]['experience'] = $item['JobsJYInput'];
                    $sub_info[$key]['education'] = $item['JobsXLInput'];
                    $sub_info[$key]['phone'] = $item['JobsDHInput'];
                    $sub_info[$key]['content'] = json_encode($item['JobsXQInput']);
                    $sub_info[$key]['t_id'] = $uid;
                }
            }
        }
        $arr = array_merge($sub_web,$sub_info);
        $thirdApp = new ThirdAppService();
        $thirdApp->addSubWebInfo($uid, $arr);//添加企业咨询
        $result = $thirdApp->updateUserInfo($uid, $web_info);
        return array($item='', $result);
    }

    /**
     * 获取小程序详情
     * @throws MiniSuiteException
     */
    public function getMiniDetail(){
        $t_id = Token::getCurrentUid();
        $mini = new ThirdAppService();
        $result = $mini->getMiniDetail($t_id);
        if (!$result){
            throw new MiniSuiteException([
                'msg'=>'获取小程序信息出错',
                'error_code'=>26017
            ]);
        }
        return $result;
    }

    /**
     * 获取企业微信信息
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws MiniSuiteException
     */
    public function getWorkDetail(){
        $t_id = Token::getCurrentUid();
        $thirdApp = new  ThirdAppService();
        $result =  $thirdApp->getAgentInfo($t_id);
        if (!$result){
            throw new MiniSuiteException([
                'msg'=>'获取企业微信信息错误',
                'error_code'=>26017
            ]);
        }else{
            $result->visible(['corp_name','corp_full_name','agents']);
            foreach ($result->agents as $value){
                $value->visible(['type']);
            }
        }
        return $result;
    }

}